package com.easyj.base.security.service;

import com.easyj.base.common.ajax.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * return 403
 * @author hjf
 *没有权限 禁止执行此操作
 */
@Service
public class AuthenticationAccessDeniedHandler implements AccessDeniedHandler {
	private ObjectMapper objectMapper=new ObjectMapper();
	
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response,
                       AccessDeniedException e) throws IOException {
    	
    	String message="你没有权限，执行此操作！";
    	Result result=Result.Error(403,message);
		String json = objectMapper.writeValueAsString(result);
	    response.setContentType("application/json;charset=UTF-8");
	    response.getWriter().write(json);
	    
       
    }
}
